﻿
using Css_VoteSys.Models;
using Css_VoteSys.Models.Database;
using Css_VoteSys.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace Css_VoteSys.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly Css_VoteSysContext _db;
        private readonly IJWTService _jwtService;
        private readonly IUserService _UserService;

        public UserController(Css_VoteSysContext db, IJWTService jwtService, IUserService UserService)
        {
            _db = db;
            _jwtService = jwtService;
            _UserService = UserService;
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="Email">邮箱</param>
        /// <param name="Password">密码</param>
        /// <returns>注册结果信息</returns>
        [HttpPost]
        public string Register(string Email, string Password)
        {

            //随机数1000-9999
            var salt = new Random().Next(1000, 9999);
            var md5 = MD5Helper.MD5Encrypt32(Password + salt.ToString());
            //判断邮箱是否重复 拿这个邮箱 去数据库查 有没有存在
            if (_db.Users.Any(x => x.Email == Email)) return "邮箱已存在";
            //把信息添加到数据库 注册成功
            _db.Users.Add(new User
            {
                Email = Email,
                Password = md5,
                PrivilegeId = 2, // 1为管理员 2为普通用户
                Salt = salt.ToString()
            });
            //保存更改
            var row = _db.SaveChanges();
            if (row > 0)
                return "注册成功";
            return "注册失败";
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="Email"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        [HttpPost]
        public string Login(string Email, string Password)
        {
            //1、使用手机号码去数据查询用户
            var user = _db.Users.FirstOrDefault(x => x.Email == Email);

            //2、判断是否空
            if (user == null)
                return "用户不存在";

            //生成md5字符串                      输入的         数据库的盐值
            var md5 = MD5Helper.MD5Encrypt32(Password + user.Salt.ToString());

            //3、判断密码是否正确md5  
            if (user.Password != md5)
                return "密码不正确";

            //4、创建token
            return _jwtService.CreateToken(user.UserId, user.Email);
        }

        /// <summary>
        /// 获取用户权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string GetPrivilegeId()
        {
            var UserId = Convert.ToInt32(Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value);
            var result = _UserService.IsRoot(UserId);
            if (result)
            {
                return "管理员";
            }
            return "普通用户";
        }
    }
}
